import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from "rxjs";

@Injectable({
  providedIn: 'root'
})
export class SelladPositionService {

  // 广告位list--URL
  public advertisingSpaceListUrl = '/advertPosition/list';

  // 修改 || 删除
  public modifyDataUrl = '/advertPosition/update';

  // 文件上传
  public uploadFileUrl = '/common/upload/file';

  // 字典查询子列表
  public dictUrl = '/dict/detail';

  // 广告查询
  public advertisementListUrl = '/advert/list';

  // 新增广告位
  public addAdvertisingSpaceUrl = '/advertPosition/save';

  // 查询广告位详情
  public queryDetailUrl = '/advertPosition/details';

  constructor(public httpClient: HttpClient) { }

  /**
   * 查询广告位tabledata
   * @param params 
   * @returns 
   */
  public queryTableData(params: any): Observable<any> {
    return this.httpClient.post(this.advertisingSpaceListUrl, params);
  }

  /**
   * 修改 || 删除
   * @param params 
   * @returns 
   */
  public modifyData(params: any): Observable<any> {
    return this.httpClient.post(this.modifyDataUrl, params);
  }

  /**
   * 新增广告位
   * @param params 
   * @returns 
   */
  public addAdvertisingSpaceData(params: any): Observable<any> {
    return this.httpClient.post(this.addAdvertisingSpaceUrl, params);
  }

  /**
   * 查询广告位详情
   * @param params 
   * @returns 
   */
  public queryDetailData(params: any): Observable<any> {
    return this.httpClient.post(this.queryDetailUrl, params);
  }

  /**
   * 文件上传
   * @param fileToUpload 
   * @returns 
   */
   public async uploadFileData(fileToUpload: File) {
    const formData: FormData = new FormData();
    formData.append('file', fileToUpload);
    formData.append('name', "adverts");
    return await this.httpClient.post(this.uploadFileUrl, formData).toPromise();
  }

  /**
   * 字典查询
   * @param parentKey 
   * @returns 
   */
  public async dictListData(parentKey: any) {
    let params: any = {
      parentKey: parentKey,
      pageNum: 0,
      pageSize: 0
    };
    return await this.httpClient.get(this.dictUrl, { params: params }).toPromise();
  }

  /**
   * 查询广告tabledata
   * @param params 
   * @returns 
   */
   public advertisementTableData(params: any): Observable<any> {
    return this.httpClient.post(this.advertisementListUrl, params);
  }
}



